package me.cxlr.qinlauncher2.view.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.SeekBar;

import androidx.appcompat.widget.AppCompatSeekBar;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;

import me.cxlr.qinlauncher2.R;

/**
 * 调整音量
 *
 * @author 酷安用户:山轮纸
 */
public class AdjusVolume {
    private volatile static AdjusVolume adjusVolume;

    private AdjusVolume() {
    }

    public static AdjusVolume getInstance() {
        if (adjusVolume == null) {
            synchronized (AdjusVolume.class) {
                if (adjusVolume == null) {
                    adjusVolume = new AdjusVolume();
                }
            }
        }
        return adjusVolume;
    }

    private PopupWindow pwVolume;
    private LinearLayoutCompat llRoot;
    private AppCompatTextView tvMedia;
    private AppCompatTextView tvRing;
    private AppCompatTextView tvAlarm;
    private AppCompatSeekBar sbMedia;
    private AppCompatSeekBar sbRing;
    private AppCompatSeekBar sbAlarm;

    @SuppressLint("InflateParams")
    public void openVolume(Activity activity) {
         View view = activity.getLayoutInflater().inflate(R.layout.dialog_adjus_volume, null);
        pwVolume = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

        llRoot = view.findViewById(R.id.dav_ll_root);
        tvMedia = view.findViewById(R.id.dav_tv_media);
        tvRing = view.findViewById(R.id.dav_tv_ring);
        tvAlarm = view.findViewById(R.id.dav_tv_alarm);
        sbMedia = view.findViewById(R.id.dav_sb_media);
        sbRing = view.findViewById(R.id.dav_sb_ring);
        sbAlarm = view.findViewById(R.id.dav_sb_alarm);

        initVolume(activity);
        pwVolume.showAsDropDown(view);
    }

    int maxMediaVolume;
    int mediaPresentVolume;
    int maxRingVolume;
    int ringPresentVolume;
    int maxAlarmVolume;
    int alarmPresentVolume;

    @SuppressLint("SetTextI18n")
    private void initVolume(Activity activity) {
        AudioManager audioManager;
        audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);

        maxMediaVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        mediaPresentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        tvMedia.setText(Integer.toString(mediaPresentVolume));
        sbMedia.setMax(maxMediaVolume);
        sbMedia.setProgress(mediaPresentVolume);
        sbMedia.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                mediaPresentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                tvMedia.setText(Integer.toString(mediaPresentVolume));
                sbMedia.setProgress(mediaPresentVolume);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        maxRingVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
        ringPresentVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
        tvRing.setText(Integer.toString(ringPresentVolume));
        sbRing.setMax(maxRingVolume);
        sbRing.setProgress(ringPresentVolume);
        sbRing.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_RING, progress, 0);
                ringPresentVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
                tvRing.setText(Integer.toString(ringPresentVolume));
                sbRing.setProgress(ringPresentVolume);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        maxAlarmVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
        alarmPresentVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
        tvAlarm.setText(Integer.toString(alarmPresentVolume));
        sbAlarm.setMax(maxAlarmVolume);
        sbAlarm.setProgress(alarmPresentVolume);
        sbAlarm.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_ALARM, progress, 0);
                alarmPresentVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
                tvAlarm.setText(Integer.toString(alarmPresentVolume));
                sbAlarm.setProgress(alarmPresentVolume);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        llRoot.setOnClickListener(v->pwVolume.dismiss());

    }

}
